package org.september.pisces.settings.controller;

import java.util.Arrays;
import java.util.Map;

import org.september.core.component.ApplicationContextHolder;
import org.september.core.exception.BusinessException;
import org.september.pisces.settings.entity.SystemSettings;
import org.september.pisces.settings.service.PiscesSettingService;
import org.september.pisces.settings.service.SettingsUpdateEventHandler;
import org.september.pisces.user.permission.service.OperationLogService;
import org.september.simpleweb.controller.BaseController;
import org.september.simpleweb.model.ResponseVo;
import org.september.smartdao.model.Page;
import org.september.smartdao.model.ParamMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 */
@RestController
public class PiscesSettingsController extends BaseController {

    @Autowired
    private PiscesSettingService systemSettingService;

    @Autowired
    private OperationLogService operationLogService;

    @RequestMapping(value = List_Page)
    public ModelAndView systemSettingList() throws Exception {
        ModelAndView mv = new ModelAndView();
        return mv;
    }

    @ResponseBody
    @RequestMapping(value = List_Page_Data)
    public ResponseVo<Page<SystemSettings>> listSettingsData(Page<SystemSettings> page, SystemSettings vo) throws Exception {
        ParamMap pm = new ParamMap(vo);
        page = systemSettingService.getCommonDao().findPageByParams(SystemSettings.class, page, "PiscesSettings.listSettings", pm);
        return ResponseVo.<Page<SystemSettings>>BUILDER().setData(page).setCode(ResponseVo.BUSINESS_CODE.SUCCESS);
    }

    @RequestMapping(value = Edit_Page)
    public ModelAndView editSetting(Long id) throws Exception {
        ModelAndView mv = new ModelAndView();
        SystemSettings po = systemSettingService.getCommonDao().get(SystemSettings.class, id);
        mv.addObject("po", po);
        return mv;
    }

    @ResponseBody
    @RequestMapping(value = Edit_Action)
    public ResponseVo<String> doUpdateSystemSetting(SystemSettings setting) throws Exception {
        SystemSettings po = systemSettingService.getCommonDao().get(SystemSettings.class, setting.getId());
        if (po.getCfgKey().contains("over_rate_level")) {
            if (!setting.getCfgVal().contains(",")) {
                throw new BusinessException("配置值格式不正确");
            }
            String[] vals = setting.getCfgVal().split(",");
            if (vals.length < 2) {
                throw new BusinessException("配置值格式不正确");
            }
            if(Double.parseDouble(vals[0]) >= Double.parseDouble(vals[1])) {
                throw new BusinessException("配置值格式不正确");
            }
        }
        String oldVal = po.getCfgVal();
        po.setCfgVal(setting.getCfgVal());
        po.setRemark(setting.getRemark());
        systemSettingService.update(po);
        operationLogService.addLog("修改了系统配置信息【" + setting.getCfgKey() + "】的值为" + setting.getCfgVal() + ",原值为" + oldVal);

        Map<String, SettingsUpdateEventHandler> handlers = ApplicationContextHolder.getContext().getBeansOfType(SettingsUpdateEventHandler.class);
        for (SettingsUpdateEventHandler hd : handlers.values()) {
            hd.onUpdated(setting);
        }
        return ResponseVo.<String>BUILDER().setData("").setCode(ResponseVo.BUSINESS_CODE.SUCCESS);
    }

    private static final String Mapping_Prefix = "/pisces/settings";
    public static final String List_Page = Mapping_Prefix + "/systemSettingList";
    public static final String List_Page_Data = Mapping_Prefix + "/listSettingsData";

    public static final String Edit_Page = Mapping_Prefix + "/editSetting";
    public static final String Edit_Action = Mapping_Prefix + "/doUpdateSetting";

}
